//
//  ImageAuthorizationViewController.swift
//  App_Common
//
//  Created by 蒋晨成 on 2022/2/10.
//

import App_Common_UIKitExtensions
import UICore
import UIKit

class ImageAuthorizationViewController: ViewController {
    lazy var textLabel: UILabel = .init()
        .x
        .font(.semiboldOrSystem(17, fontName: .pingFangSC))
        .textColor(CommonModular.color(name: "App.Light1"))
        .numberOfLines(0)
        .textAlignment(.center)
        .instance

    lazy var detailLabel: UILabel = .init()
        .x
        .font(.regularOrSystem(15, fontName: .pingFangSC))
        .textColor(CommonModular.color(name: "App.Light1"))
        .numberOfLines(0)
        .textAlignment(.center)
        .instance

    lazy var doneButton: UIButton = .init()
        .x
        .titleFont(font: .mediumOrSystem(15, fontName: .pingFangSC))
        .setTitleColor(CommonModular.color(name: "App.Dark2"), for: .normal)
        .backgroundColor(CommonModular.color(name: "App.Primary"))
        .corners(radius: 12)
        .setTitle(CommonModular.localizedString(key: "Authorization.Setting.Button.title"), for: .normal)
        .instance

    lazy var cancelButton: UIButton = .init()
        .x
        .titleFont(font: .regularOrSystem(15, fontName: .pingFangSC))
        .setTitleColor(CommonModular.color(name: "App.Light1"), for: .normal)
        .setTitle(CommonModular.localizedString(key: "Authorization.Cancel.Button.title"), for: .normal)
        .instance

    var type: ImageType

    lazy var json: [String: [String: String]] = {
        [
            "camera": [
                "title": CommonModular.localizedString(key: "Authorization.Title.Camera"),
                "message": CommonModular.localizedString(key: "Authorization.Message.Camera"),
            ],
            "photo": [
                "title": CommonModular.localizedString(key: "Authorization.Title.PhotoLibrary"),
                "message": CommonModular.localizedString(key: "Authorization.Message.PhotoLibrary"),
            ],
        ]
    }()

    public init(type: ImageAuthorizationViewController.ImageType) {
        self.type = type
        super.init(nibName: nil, bundle: nil)
    }

    @available(*, unavailable)
    required init?(coder _: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension ImageAuthorizationViewController {
    enum ImageType: String {
        case camera
        case photo
    }
}

extension ImageAuthorizationViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        setup()
    }

    override public var modalPresentationStyle: UIModalPresentationStyle {
        get {
            UIModalPresentationStyle.fullScreen
        }
        set {
            super.modalPresentationStyle = newValue
        }
    }
}

extension ImageAuthorizationViewController {
    func setup() {
        view.backgroundColor = CommonModular.color(name: "App.Dark1")
        textLabel.x.add(to: view).snp.makeConstraints {
            $0.top.equalTo(256)
            $0.centerX.leading.trailing.equalToSuperview()
        }

        detailLabel.x.add(to: view).snp.makeConstraints {
            $0.top.equalTo(textLabel.snp.bottom).offset(20)
            $0.left.equalTo(58)
            $0.right.equalTo(-58)
            $0.centerX.leading.trailing.equalToSuperview()
        }

        doneButton.x.add(to: view).snp.makeConstraints {
            $0.top.equalTo(detailLabel.snp.bottom).offset(104)
            $0.centerX.equalToSuperview()
            $0.size.equalTo(CGSize(width: 220, height: 52))
        }

        cancelButton.x.add(to: view).snp.makeConstraints {
            $0.top.equalTo(35)
            $0.left.equalTo(0)
            $0.size.equalTo(CGSize(width: 72, height: 32))
        }

        if let dict = json[type.rawValue] {
            textLabel.text = dict["title"]
            detailLabel.text = dict["message"]
        }
        doneButton.addTarget(self, action: #selector(settingButtonAction), for: .touchUpInside)
        cancelButton.addTarget(self, action: #selector(backButtonAction), for: .touchUpInside)
    }

    @objc
    func backButtonAction() {
        dismiss(animated: true, completion: nil)
    }

    @objc
    func settingButtonAction() {
        if let url = URL(string: Application.Constants.openSettingsURLString) {
            Router.applicationOpen(url, options: [:], completionHandler: nil)
        }
    }
}
